//
//  ZMSearchViewController.m
//  XMJJ_zml
//
//  Created by 杨何华 on 2025/5/10.
//

#import "ZMSearchViewController.h"
#import "ZMYTabBar.h"
#import "RoundedSearchBar.h"
#import "HistorySearchTagView.h"
@interface ZMSearchViewController ()<HistorySearchTagViewDelegate>

@property (nonatomic, strong) HistorySearchTagView *historyTagView;
@property (nonatomic, strong) NSMutableArray<NSString *> *historyTags;
@property (nonatomic, strong) UICollectionViewFlowLayout *layout;
@property (nonatomic, strong) NSArray<NSString *> *tabTitles;
@property (nonatomic, assign) NSInteger selectedTabIndex;
@property (nonatomic, strong) NSMutableArray<NSArray *> *dataSources; // 存储不同标签对应数据源
@property (nonatomic, strong) ZMYTabBar *tabBar;
@property (nonatomic, strong) UIView *contentView;
@property (nonatomic, strong) NSArray<UIView *> *contentPages;
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSArray<NSNumber *> *imageHeights;
@property (nonatomic, strong) RoundedSearchBar *searchBar;

@property (nonatomic, strong) NSMutableArray * dataArray;
@property (nonatomic, strong) NSMutableDictionary<NSString *, NSValue *> *imageSizeCache; // 缓存图片尺寸

@property (nonatomic, strong) UIButton *editButton;
@property (nonatomic, assign) BOOL isEditing;
@property (nonatomic, strong) UILabel *tagListLabel;
@end

@implementation ZMSearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self  setupSearch];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.historyTagView = [[HistorySearchTagView alloc] initWithFrame:CGRectMake(10, 110, self.view.bounds.size.width - 20, 50) maxTagCount:10];
     self.historyTagView.delegate = self;
     [self.view addSubview:self.historyTagView];
     
     // 添加一些示例标签
   
    [self.historyTagView addTag:@"Apple Watch"];
    [self.historyTagView addTag:@"AirPods"];
    [self.historyTagView addTag:@"iPhone"];
    [self.historyTagView addTag:@"iPad"];
    [self.historyTagView addTag:@"MacBook"];

     // 添加编辑按钮
     UIImage * dele = [UIImage imageNamed:@"dele"];
     self.editButton = [UIButton buttonWithType:UIButtonTypeSystem];
     self.editButton.frame = CGRectMake(self.view.bounds.size.width - 80 + 20, 110, dele.size.width, dele.size.height);
//     [self.editButton setTitle:@"编辑" forState:UIControlStateNormal];
     [self.editButton setBackgroundImage:[UIImage imageNamed:@"dele"] forState:UIControlStateNormal];
     [self.editButton addTarget:self action:@selector(editButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
     [self.view addSubview:self.editButton];
     
     // 添加标签列表显示标签顺序
     self.tagListLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, CGRectGetMaxY(self.historyTagView.frame) + 20, self.view.bounds.size.width - 40, 200)];
     self.tagListLabel.numberOfLines = 0;
     self.tagListLabel.font = [UIFont systemFontOfSize:14.0f];
     [self.view addSubview:self.tagListLabel];
     
 
    
}
- (void)editButtonTapped:(UIButton *)sender {
    self.isEditing = !self.isEditing;
    [self.historyTagView updateEditingState:self.isEditing];
    if(self.isEditing){
       [sender setTitle:@"完成" forState:UIControlStateNormal];
        UIImage * dele = [UIImage imageNamed:@""];
        [sender setBackgroundImage:dele forState:UIControlStateNormal];
        sender.frame = CGRectMake(self.view.bounds.size.width - 80 + 10, 120, dele.size.width+ 40, dele.size.height);
       }else {
        UIImage * dele = [UIImage imageNamed:@"dele"];
        [sender setTitle:@"" forState:UIControlStateNormal];
        [sender setBackgroundImage:dele forState:UIControlStateNormal];
        sender.frame = CGRectMake(self.view.bounds.size.width - 80 + 20, 110, dele.size.width, dele.size.height);
     }

}
//- (void)editButtonTapped {
//    self.isEditing = !self.isEditing;
//    [self.historyTagView updateEditingState:self.isEditing];
//    [self.editButton setTitle:self.isEditing ? @"完成" : @"编辑" forState:UIControlStateNormal];
//}

//- (void)updateTagListLabel {
//    NSArray *tags = [self.historyTagView getAllTags];
//    NSMutableString *tagListString = [NSMutableString stringWithString:@"当前标签顺序:\n"];
//    
//    for (NSInteger i = 0; i < tags.count; i++) {
//        [tagListString appendFormat:@"%ld. %@\n", (long)i+1, tags[i]];
//    }
//    
//    self.tagListLabel.text = tagListString;
//}

#pragma mark - HistorySearchTagViewDelegate

- (void)didSelectTag:(NSString *)tagName {
    NSLog(@"选中标签: %@", tagName);
    // 在这里可以实现搜索功能
}

- (void)didDeleteTag:(NSString *)tagName newTags:(NSArray<NSString *> *)newTags {
    NSLog(@"删除标签: %@", tagName);
    NSLog(@"新的标签列表: %@", newTags);
    
    // 更新标签列表显示
//    [self updateTagListLabel];
}

- (void)didClearAllTags {
    NSLog(@"清空所有标签");
    
    // 更新标签列表显示
//    [self updateTagListLabel];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
 
}
-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
 
}
-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.searchBar.searchText = @"";
 
}
-(void)setupSearch{
     
    UIButton * back = [UIButton buttonWithType:UIButtonTypeCustom];
    UIView * backView = [UIView new];
    backView.frame = CGRectMake(0, 0, 40, 40);
    back.frame = CGRectMake(0, 10, 20, 20);
    [back setBackgroundImage:[UIImage imageNamed:@"navBack"] forState:UIControlStateNormal];
    [backView addSubview:back];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:backView];
    [back addTarget:self action:@selector(backTouchEvent) forControlEvents:UIControlEventTouchUpInside];
    self.searchBar = [[RoundedSearchBar alloc] initWithFrame:CGRectMake(100, 60, self.view.bounds.size.width - 180, 30)];
    // 设置搜索回调
    [self.searchBar setSearchText:@"搜索内容/用户"];
    self.searchBar.searchAction = ^(NSString *searchText) {
        NSLog(@"搜索内容: %@", searchText);
        // 在这里执行搜索操作
        [[XMNetwork shareInstance]postDataWithPath:[NSString stringWithFormat:@"%@%@",HttpInterface,searchService] parameters:@{@"keyword":searchText,@"pageNum":@"1",@"pageSize":@"10"} needToken:NO headers:nil success:^(id  _Nonnull responseObject) {
              NSDictionary * resultDic = responseObject;
              NSArray * data = [resultDic objectForKey:@"data"];
             } failure:^(NSError * _Nonnull error) {
//               NSLog(@"%@",error.description);
            }];
      
       };
     [self.view addSubview:self.searchBar];
 
}
- (void)backTouchEvent {
    [self.navigationController popViewControllerAnimated:YES];
}
@end
